#ifndef WEBHOST_H
#define WEBHOST_H

#include "qobject.h"
#include "webpage.h"
#include "window.h"
#include "qfiledialog.h"
#include "qeventloop.h"
#include "qvariant.h"

class WebHost: public QObject {

	Q_OBJECT

private:

	static QEventLoop* eventLoop;
	static QFileDialog* fileDialog;
	QString setJSTempVariable(QObject* value);
	QString setJSTempVariable(QVariant value);
	bool fireJSEvent(QWebFrame* sourceFrame, QString eventType, QString eventDataRef = 0);
	QVariantMap dialog(QFileDialog::AcceptMode acceptMode, QVariantMap options);

protected:

	WebPage* webPage;
	static QHash<QString, QObject*> JSOTempVariable;
	static QHash<QString, QVariant> JSVTempVariable;
	bool fireEvent(QString eventType, QWebFrame* frame = 0);
	bool fireEvent(QString eventType, QObject* eventData, QWebFrame* frame = 0);
	bool fireEvent(QString eventType, QVariant eventData, QWebFrame* frame = 0);

private slots:

	void fdAccepted();
	void fdRejected();
	WebPage* windowCreate();
	void frameCreated(QWebFrame* frame);
	void javaScriptFrameObjectCleared();

public:

	explicit WebHost(QObject* parent);
	virtual ~WebHost();
	QWidget* getParentWindow();

public slots:

	QObject* getJSOTempVariable(QString key);
	QVariant getJSVTempVariable(QString key);
	void require(QString filePath);

	void openDialog(QString url = "");
	QVariantMap openFolderDialog(QVariantMap options);
	QVariantMap openFileDialog(QVariantMap options);
	QVariantMap saveFileDialog(QVariantMap options);

};

#endif // WEBHOST_H
